{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
    "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
    "execution": {
     "iopub.execute_input": "2022-07-31T06:22:21.528294Z",
     "iopub.status.busy": "2022-07-31T06:22:21.527656Z",
     "iopub.status.idle": "2022-07-31T06:22:21.534740Z",
     "shell.execute_reply": "2022-07-31T06:22:21.533143Z",
     "shell.execute_reply.started": "2022-07-31T06:22:21.528249Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "print(torch.__version__)\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.Tensor的理解：\n",
    "    1.常数，scaler:0阶张量\n",
    "    2.向量,vector,1阶张量\n",
    "    3.矩阵,matrix:2阶张量\n",
    "    4.3阶张量\n",
    "# 2. 创建张量的方法：\n",
    "     1.使用列表创建Tensor\n",
    "     2.使用numpy数组创建Tensor\n",
    "     3.通过torch的API创建Tensor\n",
    "     \n",
    "# 3. 张量的方法和属性\n",
    "    1.tensor.item(),当tensor中只有一个元素可以用的时候\n",
    "    2.Tensor转为ndarray\n",
    "    3.形状修改，tensor.view((3, 4)), 类似numpy中的reshape,是一种浅拷贝\n",
    "    4.获取维数、转置、轴滚动。\n",
    "    5.在方法后加_，会原地修改，相当于Tensorflow里的inplace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T06:27:39.559996Z",
     "iopub.status.busy": "2022-07-31T06:27:39.559579Z",
     "iopub.status.idle": "2022-07-31T06:27:39.576433Z",
     "shell.execute_reply": "2022-07-31T06:27:39.575188Z",
     "shell.execute_reply.started": "2022-07-31T06:27:39.559963Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.使用列表创建Tensor\n",
    "t1 = torch.Tensor([1, 2, 3])\n",
    "print(t1)\n",
    "\"\"\"\n",
    "tensor([1., 2., 3.])\n",
    "\"\"\"\n",
    "\n",
    "# 2.使用numpy数组创建Tensor\n",
    "array1 = np.arange(12).reshape(3, 4)\n",
    "t2 = torch.Tensor(array1)\n",
    "print(t2)\n",
    "\"\"\"\n",
    "tensor([[ 0.,  1.,  2.,  3.],\n",
    "        [ 4.,  5.,  6.,  7.],\n",
    "        [ 8.,  9., 10., 11.]])\n",
    "\"\"\"\n",
    "\n",
    "# 3.通过torch的API创建Tensor\n",
    "\"\"\" \n",
    "torch.empty(3,4)：创建3行四列的空的tensor,会用无用的数据进行填充\n",
    "torch.ones([3,4]):三行四列全为1的tensor\n",
    "torch.zeros([3,4]):三行四列全为0的tensor\n",
    "torch.rand([3,4]):三行四列随机值在[0,1]之间的值\n",
    "torch.randint(low = 0, high = 10, size = [3, 4]) 创建3*4的随机整数的Tensor，值区间：[low, high]\n",
    "torch.randn([3,4]) 均值为0，方差为1\n",
    "\"\"\"\n",
    "print(torch.empty(3,4))\n",
    "print(torch.ones([3,4]))\n",
    "print(torch.zeros([3,4]))\n",
    "print(torch.rand([3,4]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T06:29:04.995479Z",
     "iopub.status.busy": "2022-07-31T06:29:04.995033Z",
     "iopub.status.idle": "2022-07-31T06:29:05.019150Z",
     "shell.execute_reply": "2022-07-31T06:29:05.017794Z",
     "shell.execute_reply.started": "2022-07-31T06:29:04.995441Z"
    }
   },
   "outputs": [],
   "source": [
    "# 张量的方法和属性\n",
    "# 1.tensor.item(),当tensor中只有一个元素可以用的时候\n",
    "a =torch.tensor(np.arange(1))\n",
    "print(a)\n",
    "print(a.item())\n",
    "\n",
    "print(torch.Tensor([[[1]]]).item())\n",
    "\n",
    "# 2.Tensor转为ndarray\n",
    "t2 = torch.Tensor([[[1,2]]])\n",
    "print(t2.numpy()) \n",
    "\n",
    "print(t2.shape)\n",
    "print(t2.size())\n",
    "print(t2.size())\n",
    "print(t2.size(-1))  #获取某个维度的数据\n",
    "\n",
    "\n",
    "# 2.形状修改，tensor.view((3, 4)), 类似numpy中的reshape,是一种浅拷贝，仅仅形状发生改变,返回一个新的结果\n",
    "\n",
    "print(t2.size())\n",
    "print(t2.view([1,2]))\n",
    "print(t2.view([2]))\n",
    "print(t2.view([2, -1])) #\n",
    "\n",
    "#3. 获取维数\n",
    "print(t2.dim())\n",
    "\n",
    "#4.获取最大值\n",
    "print(t2.max())\n",
    "\n",
    "#5.转置\n",
    "t3 = torch.tensor([[1,3,4], [2,4,6]])\n",
    "print(t3.t())\n",
    "\n",
    "# 交换轴\n",
    "t4 = torch.tensor(np.arange(24).reshape(2,3,4))\n",
    "print(t4)\n",
    "print(\"-\"*50)\n",
    "print(t4.transpose(0,1))#交换0轴和1轴\n",
    "print(\"-\"*50)\n",
    "print(t4.permute(1, 0, 2))#交换0轴和1轴\n",
    "print(\"-\"*50)\n",
    "print(t4.permute(1, 2, 0))#交换0轴和1轴\n",
    "print(\"-\"*50)\n",
    "print(t4.permute(2, 1, 0))#交换0轴和1轴\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T06:30:31.128553Z",
     "iopub.status.busy": "2022-07-31T06:30:31.128150Z",
     "iopub.status.idle": "2022-07-31T06:30:31.140028Z",
     "shell.execute_reply": "2022-07-31T06:30:31.138517Z",
     "shell.execute_reply.started": "2022-07-31T06:30:31.128521Z"
    }
   },
   "outputs": [],
   "source": [
    "# 在方法后加_，会原地修改\n",
    "x = torch.tensor(np.arange(12).reshape(3,4),dtype = torch.int64)\n",
    "print(x)\n",
    "y= torch.ones([3,4], dtype = torch.int64)\n",
    "print(y)\n",
    "\n",
    "# a3 = torch.zeros([3,4])\n",
    "# print(a3)\n",
    "      \n",
    "x.add_(y) # add_就地修改，不加下划线的会创建一个新的tensor来存储\n",
    "print(x) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T06:30:45.464008Z",
     "iopub.status.busy": "2022-07-31T06:30:45.463595Z",
     "iopub.status.idle": "2022-07-31T06:30:45.475402Z",
     "shell.execute_reply": "2022-07-31T06:30:45.474425Z",
     "shell.execute_reply.started": "2022-07-31T06:30:45.463971Z"
    }
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "GPU中tensor的使用：\n",
    "1.实例化device， torch.device(\"cpu\" or \"cuda:0\"),选cuda:0需要torch.cuda.is_available()==true\n",
    "2.tensor.to(device),把tensor转化为CUDA支持的tensor或者CPU支持的tensor\n",
    "\"\"\"\n",
    "print(torch.cuda.is_available())\n",
    "if torch.cuda.is_available():\n",
    "    device = torch.device(\"cuda\") #cuda device对象\n",
    "    y = torch.ones_like(x, device = device) #创建一个在cuda熵的tensor\n",
    "    x = x.to(device) #把x转为cuda上的tensor\n",
    "    z = x+y\n",
    "    print(z)\n",
    "    print(z.to(\"cpu\", torch.double))\n",
    "\n",
    "# torch.tensor([1.9806], device = \"cuda:0\")\n",
    "device = torch.device(\"cpu\")\n",
    "x.to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
